package com.lp.compt.controllers;

import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import com.lp.compt.pojo.SysUserEntity;
import com.lp.compt.services.SysUserEntityService;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务类
 *
 * @author
 * @since 1.0.0
 */
@RestController
@CrossOrigin
@RequestMapping("/sysUserEntity")
public class SysUserEntityController {

    final
    SysUserEntityService sysUserEntityService;

    public SysUserEntityController(SysUserEntityService sysUserEntityService) {
        this.sysUserEntityService = sysUserEntityService;
    }

    /**
     * 根据SysUserEntity的字段,自动生成条件,字段的值为null不生成条件
     * http://localhost:8080/user/?id=1
     *
     * @param sysUserEntity 实体对象
     * @param pageable      分页/排序对象
     * @return 返回的是实体, 里面涵盖分页信息及状态码
     */
    @GetMapping
    ResponseEntity<Object> search(SysUserEntity sysUserEntity, Pageable pageable) {
        return sysUserEntityService.search(sysUserEntity, pageable);
    }

    @PostMapping("login")
    public Map login(@RequestBody Map<String, String> map) {
        SysUserEntity user = sysUserEntityService.login(map.get("phone"), map.get("password"));
        if (user == null) {
            return new HashMap() {{
                put("code", 301);
                put("msg", "用户名或密码错误");
            }};
        } else {
            return new HashMap() {{
                put("user", user);
                put("code", 200);
            }};
        }
    }

    @PostMapping("info")
    public Map upInfo(@RequestBody Map<String, String> map) {
        sysUserEntityService.info(map);
        return new HashMap() {{
            put("code", 200);
        }};
    }

    @PostMapping("getInfo")
    public Map getInfo(@RequestBody Map<String, String> map) {
        Object o = sysUserEntityService.getInfo(Integer.valueOf(map.get("id")));
        return new HashMap() {{
            put("code", 200);
            put("info", o);
        }};
    }

    @PostMapping
    void create() {
    }

    @PutMapping
    void update() {
    }

    @DeleteMapping("/")
    void delete() {
    }
}
